package com.yunhe.marketing.util;

import com.yunhe.common.util.ReflectUtil;
import com.yunhe.common.util.StringUtil;
import com.yunhe.marketing.model.EnterpriseUpload;
import com.yunhe.marketing.model.EnterpriseUploadAQC;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @Author: DongWenYu
 * @Date: 2021/3/25 14:31
 */
public class EnterpriseUploadAQCUtil {

    public static Boolean isEmpty(EnterpriseUploadAQC enterpriseUploadAQC) throws IllegalAccessException {
        if ("成功".equals(enterpriseUploadAQC.getStatus())){
            if ("-".equals(enterpriseUploadAQC.getEnterpriseName()) || "-".equals(enterpriseUploadAQC.getLicense())){
                return true;
            }else {
                Class thisClass = enterpriseUploadAQC.getClass();
                Field[] thisFields = thisClass.getDeclaredFields();
                String fieldValue = "-";
                for (Field field : thisFields){
                    ReflectionUtils.makeAccessible(field);
                    fieldValue = (String)field.get(enterpriseUploadAQC);
                    if (!"-".equals(fieldValue)){
                        return false;
                    }
                }
            }
            return true;
        }
        return true;
    }

    public static void update(EnterpriseUploadAQC target , EnterpriseUploadAQC source){
        Class thisClass = target.getClass();
        Class enterpriseUploadClass = source.getClass();

        Field[] thisFields = thisClass.getDeclaredFields();
        Field[] enterpriseUploadFields = enterpriseUploadClass.getDeclaredFields();
        for(int i=0; i<enterpriseUploadFields.length; i++){
            Field enterpriseUploadField = enterpriseUploadFields[i];
            if(Modifier.isStatic(enterpriseUploadField.getModifiers())){
                continue;
            }
            Field thisField = thisFields[i];
            if(Modifier.isStatic(thisField.getModifiers())){
                continue;
            }
            enterpriseUploadField.setAccessible(true);
            thisField.setAccessible(true);
            try {
                if( !isNull(enterpriseUploadField,source)) {
                    if (isNull(thisField,target)) {
                        thisField.set(target, enterpriseUploadField.get(source));
                    }
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static void convertToNull(EnterpriseUploadAQC enterpriseUploadAQC) throws Exception {
        Class thisClass = enterpriseUploadAQC.getClass();
        Field[] thisFields = thisClass.getDeclaredFields();
        String fieldValue = "-";
        for (Field field : thisFields){
            ReflectionUtils.makeAccessible(field);
            fieldValue = (String)field.get(enterpriseUploadAQC);
            if ("-".equals(fieldValue)){
                field.set(enterpriseUploadAQC,null);
            }
        }

    }

    private static Boolean isNull(Field enterpriseUploadField , EnterpriseUploadAQC enterpriseUploadAQC) throws IllegalAccessException {
        if( (enterpriseUploadField.get(enterpriseUploadAQC) == null &&  !"serialVersionUID".equals(enterpriseUploadField.getName().toString()))
                || (StringUtil.isEmpty(enterpriseUploadField.get(enterpriseUploadAQC).toString())  &&  !"serialVersionUID".equals(enterpriseUploadField.getName().toString()))){
            return true;
        }
        else return false;
    }

    public static boolean isEmpty(String str) {
        return str == null || "".equals(str) || "-".equals(str);
    }
}
